package com.smart.support.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.file.UploadBody;
import com.smart.common.entities.file.UploadResult;
import com.smart.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 *
 * @author Administrator
 * @date 2022-09-21
 */

@Api(tags = "文件上传公共资源")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "/upload/support")
@RequiredArgsConstructor

public class UploadApi {


    private final FileService fileService;


    /**
     * 二进制上传文件
     *
     * @param file 文件
     * @return {@code AjaxResult}
     */
    @PostMapping(value = "form-data-file", headers = SmartConstant.FILE_CONTENT_TYPE)
    @ApiOperation(value = "二进制上传文件")
    @SaCheckLogin
    public AjaxResult<UploadResult> upload(@RequestParam("file") MultipartFile file) {
        UploadResult uploadResult = fileService.uploadFile(file, "", "");
        return AjaxResult.data(uploadResult);
    }


    /**
     * BASE64上传文件
     *
     * @param dto dto
     * @return {@code AjaxResult}
     */
    @PostMapping("base64-file")
    @ApiOperation(value = "BASE64上传文件")
    @SaCheckLogin
    public AjaxResult<UploadResult> uploadFileByBase64(@RequestBody @Validated UploadBody.ByteFile dto) {
        UploadResult uploadResult = fileService.uploadFileByByte(dto, "", "");
        return AjaxResult.data(uploadResult);
    }

    /**
     * 二进制上传图片
     *
     * @param file 文件
     * @return {@code AjaxResult}
     */
    @PostMapping("form-data-image")
    @ApiOperation(value = "二进制上传图片")
    @SaCheckLogin
    public AjaxResult<UploadResult> uploadImage(@RequestParam("file") MultipartFile file) {
        UploadResult uploadResult = fileService.uploadImg(file, "", "");
        return AjaxResult.data(uploadResult);

    }


    /**
     * BASE64上传图片
     *
     * @param dto dto
     * @return {@code AjaxResult}
     */
    @PostMapping("base64-image")
    @ApiOperation(value = "BASE64上传图片")
    @SaCheckLogin
    public AjaxResult<UploadResult> uploadImageByBase64(@RequestBody @Validated UploadBody.Base64 dto) {
        UploadResult uploadResult = fileService.uploadFile(dto, "", "",true);
        return AjaxResult.data(uploadResult);

    }


}
